package com.modern.js.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Role;
import com.modern.js.mapper.RoleMapper;
import com.modern.js.pojo.AssistPojo;
import com.modern.js.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.modern.js.utils.StringUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<AssistPojo> roleById(Integer roleId) {
        List<AssistPojo> list = roleMapper.roleById(roleId);
        return list;
    }

    @Override
    public IPage<AssistPojo> selectAll(Page<AssistPojo> page,String deptName,String roleName) {
        return roleMapper.selectAll(page,deptName,roleName);
    }

    @Override
    public List<AssistPojo> selectRoleDept(Integer deptId) {
        List<AssistPojo> list = roleMapper.selectRoleDept(deptId);

        return list;
    }

    /**
     * 添加角色
     * @param
     * @return
     */
    public boolean insertRole(Role role, Integer deptItd){

        role.setCreateTime(StringUtils.getNowTime());
        role.setUpdateTime(StringUtils.getNowTime());
        role.setIsDel(1);
        int result = roleMapper.insert(role);
        AssistPojo assistPojo = new AssistPojo();
        assistPojo.setRoleId(role.getRoleId());
        System.out.println("获取角色的id："+role.getRoleId());
        assistPojo.setDeptId(deptItd);
        assistPojo.setCreateTime(role.getCreateTime());
        assistPojo.setUpdateTime(role.getUpdateTime());
        assistPojo.setIsDel(1);
        result+=roleMapper.addRoleDept(assistPojo);
        if (result==2){
            System.out.println("添加成功!");
            return true;
        }else {
            System.out.println("添加失败！");
            return false;
        }
    }

    @Override
    public int updateRole(Role role,Integer deptId,Integer id) {
        role.setUpdateTime(StringUtils.getNowTime());
        int result = roleMapper.updateById(role);
        AssistPojo assistPojo = new AssistPojo();
        assistPojo.setId(id);
        assistPojo.setUpdateTime(role.getUpdateTime());
        assistPojo.setRoleId(role.getRoleId());
        assistPojo.setDeptId(deptId);
        result+=roleMapper.updateRoleDept(assistPojo);
        if (result==2){
            System.out.println("修改成功！");
        }else {
            System.out.println("修改失败！");
        }
        return result;
    }


    @Override
    public boolean delRole(AssistPojo assistPojo) {
        Role role = new Role();
        role.setIsDel(0);
        role.setRoleId(assistPojo.getRoleId());
        int result = roleMapper.updateById(role);
        result+= roleMapper.deleteRoleDept(assistPojo);
        if (result==2){
            System.out.println("删除成功！");
            return true;
        }else {
            System.out.println("删除失败！");
            return false;
        }
    }
}
